package com.study.talk.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.module.entity.MomentLike;
import com.study.talk.module.entity.vo.MomentLikeVo;
import com.study.talk.module.entity.vo.UpdateLikeStateVo;
import com.study.talk.module.service.MomentCommentService;
import com.study.talk.module.service.MomentLikeService;
import com.study.talk.module.service.MomentService;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author thor
 * @since 2024-3-13
 */
@RestController
@RequestMapping("/moment_like")
public class MomentLikeController {
    @Resource
    private MomentService momentService;

    @Resource
    private MomentLikeService momentLikeService;

    @Resource
    private MomentCommentService momentCommentService;

    @GetMapping("/get/{id}")
    @ApiOperation("获取点赞记录")
    public FwResult<MomentLike> getMomentLike(@PathVariable("id") Integer id) {
        MomentLike momentLike = momentLikeService.getById(id);
        if (ObjectUtils.isNotNull(momentLike)) return FwResult.ok(momentLike);
        else return FwResult.failed();
    }

    @PostMapping("/add")
    @ApiOperation("添加点赞记录")
    public FwResult<Integer> addMomentLike(@RequestBody MomentLikeVo momentLikeVo) {
        QueryWrapper<MomentLike> wrapper = new QueryWrapper<>();
        wrapper.eq("moment_id",momentLikeVo.getMomentId())
                .eq("user_id",momentLikeVo.getUserId());
        MomentLike momentLike = momentLikeService.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(momentLike)){
            momentLike.setLikeState(momentLikeVo.getLikeState());
            momentLikeService.updateById(momentLike);
            return FwResult.ok(momentLike.getLikeState());
        }else {
            if (momentLikeService.save(new MomentLike(momentLikeVo))) {
                return FwResult.ok(momentLikeService.getOne(wrapper).getLikeState());
            }
            return FwResult.failed();
        }

    }

    @DeleteMapping("/delete")
    @ApiOperation("删除点赞记录")
    public FwResult<Boolean> deleteMomentLike(@RequestBody MomentLike momentLike) {
        if (momentLikeService.removeById(momentLike)) {
            return FwResult.ok(true);
        }
        return FwResult.failed();
    }

    @PutMapping("/update")
    @ApiOperation("删除点赞记录")
    public FwResult<Boolean> updateMomentLike(@RequestBody MomentLike momentLike) {
        if (momentLikeService.updateById(momentLike)) {
            return FwResult.ok(true);
        }
        return FwResult.failed();
    }

    @GetMapping("/get/list/{momentId}")
    @ApiOperation("根据动态id查找点赞列表")
    public FwResult<ArrayList<MomentLike>> getListByMomentId(@PathVariable("momentId") Integer momentId) {
        return FwResult.ok(momentLikeService.getListByMomentId(momentId));
    }

    @PutMapping("/update/like_state")
    @ApiOperation("修改点赞状态")
    public FwResult<Integer> updateLikeState(@RequestBody UpdateLikeStateVo updateLikeStateVo){
        momentLikeService.updateLikeState(updateLikeStateVo.getMomentId(),updateLikeStateVo.getUserId(),updateLikeStateVo.getLikeState());
        return FwResult.ok(updateLikeStateVo.getLikeState());
    }



}
